<template>
  <div class="permissions-container">
    <div class="permissions-header">
      <h3>权限管理</h3>
      <el-button type="primary" @click="showAddUserDialog">添加用户</el-button>
    </div>

    <el-table :data="users" style="width: 100%" border>
      <el-table-column prop="username" label="用户名" width="180" />
      <el-table-column prop="role" label="角色">
        <template #default="scope">
          <el-tag :type="scope.row.role === 'admin' ? 'danger' : 'primary'">
            {{ scope.row.role === 'admin' ? '管理员' : '普通用户' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="lastLogin" label="最后登录时间" width="180" />
      <el-table-column prop="status" label="状态">
        <template #default="scope">
          <el-switch
            v-model="scope.row.status"
            inline-prompt
            :active-value="true"
            :inactive-value="false"
            active-text="启用"
            inactive-text="禁用"
            @change="updateUserStatus(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="220">
        <template #default="scope">
          <el-button type="primary" link @click="editUser(scope.row)">编辑</el-button>
          <el-button type="danger" link @click="deleteUser(scope.row)">删除</el-button>
          <el-button type="warning" link @click="resetPassword(scope.row)">重置密码</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogVisible" :title="isEdit ? '编辑用户' : '添加用户'" width="30%">
      <el-form :model="userForm" label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="userForm.username" :disabled="isEdit" />
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="userForm.role" style="width: 100%">
            <el-option label="管理员" value="admin" />
            <el-option label="普通用户" value="user" />
          </el-select>
        </el-form-item>
        <el-form-item label="密码" v-if="!isEdit">
          <el-input v-model="userForm.password" type="password" />
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="userForm.status"
            inline-prompt
            :active-value="true"
            :inactive-value="false"
            active-text="启用"
            inactive-text="禁用"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="saveUser">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';

const users = ref([
  {
    id: 1,
    username: 'admin',
    role: 'admin',
    lastLogin: '2024-03-22 14:30:25',
    status: true
  },
  {
    id: 2,
    username: 'user',
    role: 'user',
    lastLogin: '2024-03-21 09:15:10',
    status: true
  }
]);

const dialogVisible = ref(false);
const isEdit = ref(false);
const userForm = ref({
  id: '',
  username: '',
  role: 'user',
  password: '',
  status: true
});

const showAddUserDialog = () => {
  isEdit.value = false;
  userForm.value = {
    id: '',
    username: '',
    role: 'user',
    password: '',
    status: true
  };
  dialogVisible.value = true;
};

const editUser = (user) => {
  isEdit.value = true;
  userForm.value = { ...user };
  dialogVisible.value = true;
};

const saveUser = () => {
  if (!userForm.value.username) {
    ElMessage.warning('请输入用户名');
    return;
  }
  
  if (!isEdit.value && !userForm.value.password) {
    ElMessage.warning('请输入密码');
    return;
  }
  
  if (isEdit.value) {
    const index = users.value.findIndex(u => u.id === userForm.value.id);
    if (index !== -1) {
      users.value[index] = { ...userForm.value };
    }
  } else {
    // 检查用户名是否已存在
    if (users.value.some(u => u.username === userForm.value.username)) {
      ElMessage.warning('用户名已存在');
      return;
    }
    
    users.value.push({
      ...userForm.value,
      id: Date.now(),
      lastLogin: '从未登录'
    });
  }
  
  dialogVisible.value = false;
  ElMessage.success(isEdit.value ? '编辑成功' : '添加成功');
};

const deleteUser = (user) => {
  if (user.username === 'admin') {
    ElMessage.warning('管理员账号不能删除');
    return;
  }
  
  ElMessageBox.confirm(
    `确定要删除用户 ${user.username} 吗？`,
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    users.value = users.value.filter(u => u.id !== user.id);
    ElMessage.success('删除成功');
  }).catch(() => {});
};

const resetPassword = (user) => {
  ElMessageBox.confirm(
    `确定要重置 ${user.username} 的密码吗？`,
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    // 在实际应用中，这里应该调用API重置密码
    ElMessage.success(`密码已重置为"1"`);
  }).catch(() => {});
};

const updateUserStatus = (user) => {
  if (user.username === 'admin' && !user.status) {
    ElMessage.warning('管理员账号不能禁用');
    user.status = true;
    return;
  }
  
  ElMessage.success(`${user.username} 已${user.status ? '启用' : '禁用'}`);
};
</script>

<style scoped>
.permissions-container {
  padding: 20px;
  background-color: #f9fbff;
  min-height: 100%;
}

.permissions-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.permissions-header h3 {
  color: #1e5eff;
  font-size: 1.5rem;
}

:deep(.el-table) {
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 20px;
}

:deep(.el-button--primary) {
  background-color: #1e5eff;
}

:deep(.el-tag--danger) {
  background-color: #fef0f0;
}

:deep(.el-tag--primary) {
  background-color: #e6f1ff;
  border-color: #1e5eff;
  color: #1e5eff;
}
</style> 